<?php
namespace app\tools\logic;
use think\facade\Db;

class MemberLogic{
	static function getList(){
		$user_name = input('user_name','');
		$level = input('level', 0);
		$page_size = input('page_size', 10);

		$start_time = input('start_time', '');
		$end_time = input('end_time', '');
		$start_time && $start_time .= " 00:00:00";
		$end_time && $end_time .= "23:59:59";

		//邮箱消费总金额
		$esql = "SELECT member_id, sum(pay_amount) as pay_amount FROM dg_order_email WHERE status=20 GROUP BY member_id";
		//短信消费总金额
		$ssql = "SELECT member_id, sum(pay_amount) as pay_amount FROM dg_order_sms WHERE status=20 GROUP BY member_id";
		//充值信息
		$csql = "SELECT member_id,sum(cz) AS cz, sum(dj) AS dj, sum(hd) AS hd, sum(yq) AS yq, sum(sc) AS sc
				   FROM (
						SELECT member_id,amount_type,init_amount,current_amount,
							 CASE amount_type WHEN 100 THEN init_amount ELSE 0 END AS cz,
							 CASE amount_type WHEN 105 THEN init_amount ELSE 0 END AS dj,
							 CASE amount_type WHEN 110 THEN init_amount ELSE 0 END AS hd,
							 CASE amount_type WHEN 115 THEN init_amount ELSE 0 END AS yq,
							 CASE amount_type WHEN 120 THEN init_amount ELSE 0 END AS sc 	
						 FROM dg_member_account_sub) a
				GROUP BY member_id";

		$obj = Db::name('member')
					->alias('m')
					->field('m.id,m.head_img,m.user_name,m.email,
							 m.status,m.invitation_code,m.grade_level,
							 mg.grade_name,mg.grade_ico,
							 #round(ma.recharge_total_amount,2) as recharge_total_amount,
							 round(ma.total_amount,2) as total_amount,
							 round(IFNULL(sm.pay_amount,0),2) as sm_amount,
							 round(IFNULL(em.pay_amount,0),2) as em_amount,
							 round(IFNULL(cz.cz,0),2) as cz_amount,
							 round(IFNULL(cz.dj,0),2) as dj_amount,
							 round(IFNULL(cz.hd,0),2) as hd_amount,
							 round(IFNULL(cz.yq,0),2) as yq_amount,
							 round(IFNULL(cz.sc,0),2) as sc_amount
							 ')
					->join('member_grade mg','mg.level=m.grade_level and mg.status<>127','left')
					->join('member_account ma','ma.member_id=m.id')
					->join("($ssql) sm",'sm.member_id=m.id','left')
					->join("($esql) em",'em.member_id=m.id','left')
					->join("($csql) cz",'cz.member_id=m.id','left')
					->where('m.status','<>',127);
		if($user_name){
			$obj->where('m.user_name|m.qq|m.email','like','%'.$user_name.'%');
		}

		//等级
		if($level){
			$obj->where('grade_level', $level);
		}

		//时间
		if($start_time){
			$obj->where('m.create_time','>=', $start_time);
		}
		if($end_time){
			$obj->where('m.create_time','<=', $end_time);
		}

		$list = $obj->order('m.id desc')
					->paginate($page_size);
		return $list;
	}


	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('member')
					->alias('m')
					->field('m.id,m.head_img,m.user_name,m.email,
							 m.qq,m.status,m.invitation_code,m.api_key,m.grade_level,
							 mg.grade_name,mg.grade_ico,
							 ma.recharge_total_amount,ma.total_amount')
					->join('member_grade mg','mg.level=m.grade_level and mg.status=1','left')
					->join('member_account ma','ma.member_id=m.id')
					->where('m.status','<>',127)
					->where('m.id', $id)
					->order('m.id desc')
					->find();
		return $info;
	}

	static function upApiKey(){
		$id = input('id', 0);
		if(!$id){
			return 110;
		}
		$api_key = trim(input('api_key', ''));
		if(!$api_key){
			return 149;
		}

		$info = Db::name('member')->where('id', $id)->find();
		if(!$info){
			return 106;
		}

		$rs = Db::name('member')->where('id', $id)->update(['api_key'=>$api_key]);
		if($rs!==false){
			return 0;
		}else{
			return 148;
		}
	}

	static function upPassword(){
		$id = input('id', 0);
		if(!$id){
			return 110;
		}
		$password = trim(input('password', ''));
		if(!$password){
			return 114;
		}

		$info = Db::name('member')->where('id', $id)->find();
		if(!$info){
			return 106;
		}

		$md5pass = md5(md5($password).$info['salt']);
		$rs = Db::name('member')->where('id', $id)->update(['password'=>$md5pass]);
		if($rs!==false){
			return 0;
		}else{
			return 148;
		}
	}

	static function delete(){
		$id =  input('id', 0);
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('member')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}

	}


	static function getGradeList(){
		$page_size = input('page_size', 10);
		$list = Db::name('member_grade')
					->field('*')
					->order('level asc, id asc')
					->paginate($page_size);
		return $list;
	}

	static function gradeGetInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('member_grade')
					->field('*')
					->where('id', $id)
					->find();
		return $info;
	}

	static function gradeAdd(){
		$grade_name = input('grade_name');
		$status = input('status', 1);
		if(!$grade_name){
			return 123;
		}
		
		$data = [];
		$data['grade_name'] = $grade_name;
		$data['grade_name_en'] = input('grade_name_en','');
		$data['status']	= $status;
		$data['level'] = input('level',1);
		$data['grade_ico'] = input('grade_ico', '');
		$data['recharge_amount'] = input('recharge_amount', 0);
		$data['content'] = input('content', '');
		$data['content_en'] = input('content_en','');
		$data['gift_rate'] = input('gift_rate', 0);
		$data['invitation_rate'] = input('invitation_rate', 0);
		$data['from_invitation_rate'] = input('from_invitation_rate', 0);
		// $data['pay_amount'] = input('pay_amount', 0);
		// $group_menu_ids && $data['group_menu_ids'] = json_encode($group_menu_ids);
		$data['create_time'] = date('Y-m-d H:i:s');

		//检查会员等级是否已经存在了
		$info = Db::name('member_grade')
				->field('id,level')
				->where('level', $data['level'])
				->find();
		if($info){
			return 127;
		}

		// print_r($data);exit;
		$rs = Db::name('member_grade')->insert($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function gradeSave(){
		$grade_name = input('grade_name');
		// $group_menu_ids = input('group_menu_ids');
		$status = input('status', 1);
		$id = input('id', 0);
		if(!$id){
			return 110;
		}
		if(!$grade_name){
			return 123;
		}

		
		$data = [];
		$data['grade_name'] = $grade_name;
		$data['grade_name_en'] = input('grade_name_en','');
		$data['status']	= $status;
		$data['level'] = input('level',1);
		$data['grade_ico'] = input('grade_ico', '');
		$data['recharge_amount'] = input('recharge_amount', 0);
		$data['gift_rate'] = input('gift_rate', 0);
		$data['content'] = input('content', '');
		$data['content_en'] = input('content_en','');
		$data['invitation_rate'] = input('invitation_rate', 0);
		$data['from_invitation_rate'] = input('from_invitation_rate', 0);
		// $data['pay_amount'] = input('pay_amount', 0);
		// $group_menu_ids && $data['group_menu_ids'] = json_encode($group_menu_ids);
		// $data['create_time'] = date('Y-m-d H:i:s');
		//检查会员等级是否已经存在了
		$info = Db::name('member_grade')
				->field('id,level')
				->where('level', $data['level'])
				->where('id','<>',$id)
				->find();
		if($info){
			return 127;
		}
		// print_r($data);exit;
		$rs = Db::name('member_grade')->where('id', $id)->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	//删除
	static function gradeDelete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		if(in_array(1, $ids)){
			return 11;
		}
		$rs = Db::name('member_grade')
				->where('id','in', $ids)
				->delete();
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

}